package com.ftg.learn.chapter07;

import static java.lang.Character.charCount;
import static java.lang.Character.toUpperCase;

/**
 * 数组联系2
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/7/18 13:25
 */

public class StringTest02 {

    public static void q1(){
        //            编写一个程序，将下面的一段文本中的各个单词的字母顺序翻转
//“To  be or not to and”,变成“oT eb ro ton ot dna”
        String str="To be or not to and";
        String[] str1=str.split(" ");
        String str2="";
        StringBuilder str3;
        for (int i = 0; i < str1.length; i++) {
           str3=new StringBuilder(str1[i]).reverse();
            str2+=str3+" ";
            }
            System.out.print(str2);
     }


    public static void q2(){
//        编写程序将String类型字符串“test”变为“tset”
        String str="test";
//        String str1=str.substring(1,3);
        char[] c=str.toCharArray();
        System.out.println(c);
        char temp=' ';
        temp=c[1];
        c[1]=c[2];
        c[2]=temp;
        System.out.println(c);
    }

    public static void q3(){
//        "轻轻的我走了， 正如我轻轻的来；我轻轻的招手，作别西天的云彩。"最后一次出现"轻"的位置
        String str="轻轻的我走了， 正如我轻轻的来；我轻轻的招手，作别西天的云彩";
        char[] c=str.toCharArray();
        int index=0;
        for (int i = 0; i < c.length; i++) {
            if(c[i]=='轻'){
                index=i;
            }
        }
        System.out.println(index);
    }

    public static void q4(){
//        求字符串“powerful heaven dragon”中有多少种字符
        String str="powerful heaven dragon";
        char[] c=str.toCharArray();
        int count=0;
        for (int i = 0; i < c.length; i++) {
//            for (int j = i; j < c.length-1-i; j++) {
//                while (c[i]==c[j]){
//                    count++;
//                }
//            }

        }
        System.out.println(count);
    }

    public static void q5(){
//        字符串“peter piper picked a peck of pickled peppers”中有多少以p开头的单词
        String str="peter piper picked a peck of pickled peppers";
        int num=0;
        if(str.charAt(0)=='p'){
            num++;
        }
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i)==' '){
                if(str.charAt(i+1)=='p'){
                    num++;
                }
            }
        }
        System.out.println(num);
    }

    public static void q6(){
//        输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
        String str="a BB2- +";
        int knum=0;
        int ynum=0;
        int snum=0;
        int qnum=0;
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            if(c==' '){
                knum++;
            }else if(c>=65&&c<=90||c>=97&&c<=122){
                ynum++;
            }else if(c>=48&&c<=57){
                snum++;
            }else {
                qnum++;
            }
        }
        System.out.println("空格有"+knum+"个\t"+"英文字母有"+ynum+"个\t"+"数字有"+snum+"个\t"+"其他字符有"+qnum+"个");
    }

    public static void q7(){
//        求长度：int myStrLen(const char  *str)
        String str="int myStrLen(const char  *str)";
        int len=0;
        for (int i = 0; i < str.length(); i++) {
            len++;
        }
        System.out.println("长度为"+ len);
    }

    public static void q8(){
//        把 lengendary 改成间隔大写小写模式，即 LeNgEnDaRy
        String str="lengendary";
        char[] c=str.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(i%2==0){
               c[i]=toUpperCase(c[i]);
            }
        }
        System.out.println(c);
    }

    public static void q9(){
//        把 lengendary 最后一个字母变大写
        String str="lengendary";
        char[] a=str.toCharArray();
        a[a.length - 1] = Character.toUpperCase(a[a.length-1]);
        System.out.println(a);
    }

    public static void q10(){
//        把最后一个two单词首字母大写
//        Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak
    String str="Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
        char[] c=str.toCharArray();
        int a=0;
        for (int i = 0; i <c.length; i++) {
            if(c[i]=='t'&&c[i+1]=='w'&&c[i+2]=='o'){
                a=i;
            }
        }
        c[a]=Character.toUpperCase(c[a]);
        System.out.println(c);
    }

    public static void main(String[] args) {
//        q1();
//        q2();
//        q3();
//        q4();
//        q5();
//        q6();
//            q7();
//        q8();
        q9();
//        q10();
    }
}
